import { message } from 'antd';
import moment from 'moment';
import {
  getInsData,
  getAccuData,
  getRcrdListData,
  getAccuListData,
  getInsListData,
} from '@/services/analysisos';

export default {
  state: {
    insManageList: [],
    accuManageList: [],
    addTotalNum: 0,
    deduTotalNum: 0,
    adjTotalNum: 0,
    serTotalNum: 0,
    insCardTotalNum: 0,
    rcrdHandleCount: 0,
    accuHandleCount: 0,
    insHandleCount: 0,
    insHandelList: [],
    accuHandleList: [],
    rcrdHandleList: [],
    dateArr: [moment().subtract(11, 'months'), moment()],
    insDate: [moment(), moment()],
    accuDate: [moment(), moment()],
    rcrdDate: [moment(), moment()],
    activeKey: '1',
    insTopListArr: [],
    accuTopListArr: [],
  },
  effects: {
    // 查询用户基本信息
    *fetchList({ payload }, { call, put, select }) {
      const activeKey = yield select(state => state.analysisos.activeKey);
      const response = yield call(
        Number(activeKey) === 1 ? getInsData : getAccuData,
        payload,
        activeKey
      );
      if (response.success) {
        yield put({
          type: 'getListData',
          payload: { ...response.data, activeKey },
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    *fetchInsList({ payload }, { call, put }) {
      const response = yield call(getInsListData, payload);
      if (response.success) {
        yield put({
          type: 'getInsListData',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    *fetchAccuList({ payload }, { call, put }) {
      const response = yield call(getAccuListData, payload);
      if (response.success) {
        yield put({
          type: 'getAccuListData',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    *fetchRcrdList({ payload }, { call, put }) {
      const response = yield call(getRcrdListData, payload);
      if (response.success) {
        yield put({
          type: 'getRcrdListData',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
  },

  reducers: {
    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    getListData(state, { payload }) {
      const { list } = payload;

      const addListData = list.map(item => ({
        type: '增员',
        month: moment(item.staYm).format('M月'),
        num: item.addStaNum,
      }));

      const deduListData = list.map(item => ({
        type: '减员',
        month: moment(item.staYm).format('M月'),
        num: item.deduStaNum,
      }));
      const adjListData = list.map(item => ({
        type: '在职调整',
        month: moment(item.staYm).format('M月'),
        num: item.adjStaNum,
      }));
      const serListData = list.map(item => ({
        type: '服务办理',
        month: moment(item.staYm).format('M月'),
        num: item.serStaNum,
      }));
      const insCardListData = list.map(item => ({
        type: '社保卡办理',
        month: moment(item.staYm).format('M月'),
        num: item.insCardStaNum,
      }));
      const topListArr = [
        {
          name: '增员办理总量',
          color: '#2593fc',
          addTotalNum: 0,
        },
        {
          name: '减员办理总量',
          color: '#38c060',
          deduTotalNum: 0,
        },
        {
          name: '在职调整办理总量',
          color: '#fdebb5',
          adjTotalNum: 0,
        },
        {
          name: '服务办理总量',
          color: '#233471',
          serTotalNum: 0,
        },
        {
          name: '社保卡办理总量',
          color: '#8e59df',
          insCardTotalNum: 0,
        },
      ];

      topListArr.forEach((item, index, arr) => {
        Object.keys(payload).forEach(value => {
          // eslint-disable-next-line no-param-reassign
          arr[index] = { name: item.name, color: item.color, totalNum: payload[value] };
        });
      });

      // Object.keys(payload).forEach(item => {
      //   switch (item) {
      //     case 'addTotalNum':
      //       topListArr.push({ totalNum: payload[item], name: '增员办理总量', color: '#2593fc' });
      //       break;
      //     case 'deduTotalNum':
      //       topListArr.push({ totalNum: payload[item], name: '减员办理总量', color: '#fdebb5' });
      //       break;
      //     case 'adjTotalNum':
      //       topListArr.push({
      //         totalNum: payload[item],
      //         name: '在职调整办理总量',
      //         color: '#38c060',
      //       });
      //       break;
      //     case 'serTotalNum':
      //       topListArr.push({ totalNum: payload[item], name: '服务办理总量', color: '#8e59df' });
      //       break;
      //     case 'insCardTotalNum':
      //       topListArr.push({ totalNum: payload[item], name: '社保卡办理总量', color: '#233471' });
      //       break;
      //   }
      // });

      return {
        ...state,

        insManageList: [
          ...addListData,
          ...deduListData,
          ...adjListData,
          ...serListData,
          ...insCardListData,
        ],
        accuManageList: [...addListData, ...deduListData, ...adjListData, ...serListData],
        insTopListArr: topListArr,
        accuTopListArr: topListArr.filter(item => item.name !== '社保卡办理总量'),
      };
    },
    getRcrdListData(state, { payload }) {
      let salesData = [];
      salesData = payload.list.map(item => ({
        x: item.serName,
        y: item.staNum,
      }));
      return {
        ...state,
        rcrdHandleList: salesData,
        rcrdHandleCount: payload.totalNum,
      };
    },
    getAccuListData(state, { payload }) {
      let salesData = [];
      salesData = payload.list.map(item => ({
        x: item.serName,
        y: item.staNum,
      }));
      return {
        ...state,
        accuHandleList: salesData,
        accuHandleCount: payload.totalNum,
      };
    },
    getInsListData(state, { payload }) {
      let salesData = [];
      salesData = payload.list.map(item => ({
        x: item.serName,
        y: item.staNum,
      }));
      return {
        ...state,
        insHandelList: salesData,
        insHandleCount: payload.totalNum,
      };
    },
  },
};
